//
//  MainTabBarController.m
//  Weibo
//
//  Created by student on 16/4/22.
//  Copyright © 2016年 BulidFuture. All rights reserved.
//

#import "MainTabBarController.h"
#import "ProfileViewController.h"
#import "DiscoverViewController.h"
#import "MessageViewController.h"
#import "HomeViewController.h"
@interface MainTabBarController ()

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1. 设置当前控制器下的tabBar的颜色
    self.tabBar.tintColor=[UIColor orangeColor];
   
  //2.添加子视图控制器
    [self addChildViewControllers];


}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //3.添加中间按钮
  
    [self addComposeBtn];
}
// 添加子视图控制器
- (void)addChildViewControllers
{
    
    //改成读取JSON,根据数据创建视图控制器
    NSString *path=[[NSBundle mainBundle]pathForResource:@"MainVCSettings" ofType:@"json"];
    NSData *data=[NSData dataWithContentsOfFile:path];
    
    NSArray *arr=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    for (NSDictionary *dict in arr) {
        NSLog(@"%@ %@ %@",dict[@"vcName"],dict[@"title"],dict[@"imageName"]);
        
        [self addChildViewController:dict[@"vcName"] title:dict[@"title"] imageName:dict[@"imageName"]];

    }
    
    
    
//    [self addChildViewController:[[HomeViewController alloc] init] title:@"首页" imageName:@"tabbar_home"];
//    [self addChildViewController:[[MessageViewController alloc] init] title:@"消息" imageName:@"tabbar_message_center"];
//    [self addChildViewController:[[UIViewController alloc] init] title:@"" imageName:@""];
//    [self addChildViewController:[[DiscoverViewController alloc] init] title:@"发现" imageName:@"tabbar_discover"];
//    [self addChildViewController:[[ProfileViewController alloc] init] title:@"我" imageName:@"tabbar_profile"];

}
//封装一下
//-(void)addChildViewController:(UIViewController *)vc
//                        title:(NSString *)title
//                    imageName:(NSString *)imageName
-(void)addChildViewController:(NSString *)vcName
                        title:(NSString *)title
                    imageName:(NSString *)imageName
{
    
    //由类名(字符串) =>对象
    UIViewController *vc=[[NSClassFromString(vcName)alloc]init];
    //1.设置其tabbarItem上的图标
    vc.tabBarItem.image=[UIImage imageNamed:imageName];
    vc.tabBarItem.selectedImage=[UIImage imageNamed:[imageName stringByAppendingString:@"_highlighted"]];
    //2. 设置其tabbarItem上的文字
    //    vc.tabBarItem.title=title;
    //3.将其嵌入到一个导航栏控制器中
    UINavigationController *navi=[[UINavigationController alloc]initWithRootViewController:vc];
    //4.设置导航栏上的标题
    //    vc.navigationItem.title=title;
    //注：2. 和 4. 如果一样，可以用以下一行替代
    vc.title = title;
    
    //5. 将导航栏控制器添加到tabBar
    [self addChildViewController:navi];  // <=  修改
    
    
    
}
//添加中间的按钮
-(void)addComposeBtn
{
    UIButton *btn =[[UIButton alloc]init];
    
    //图片
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    
    //背景图片
    
    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    
    //设置尺寸位置
    int width=[UIScreen mainScreen].bounds.size.width/self.viewControllers.count;//按钮宽度
    btn.frame=CGRectMake(width*2, 0, width, 49);
    
    
    //按钮添加事件
    [btn addTarget:self action:@selector(composeBtnPressed) forControlEvents:UIControlEventTouchUpInside];
    
    //添加到 tabbar上
    [self.tabBar addSubview:btn];   // <= 修改
}

#pragma mark - 控件事件处理
- (void)composeBtnPressed
{
    NSLog(@"%s",__func__);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
